<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Like
 * @description : like & unlike system
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Like extends AUTH_Controller {

  /**
   * Like or Unlike a post (Ajax)
   * 
   */
  public function index() {

	// Checking request type
	if ($this->is_post() && $this->is_ajax()) {

	  // Block liking operation for non user
	  if ($this->current_user['user_is_user'] == 0) {
		return;
	  }

	  // Init vars
	  $response = array('status' => '0', 'count' => '0', 'data' => '');
	  $like_types_arr = array('tweet', 'reply', 'event', 'album', 'photo', 'video', 'wall_post', 'poll', 'comment');

	  // Getting inputs
	  $like_type = $this->input->post('like_type', TRUE);
	  $like_type_id = $this->input->post('like_type_id', TRUE);
	  $like_action = $this->input->post('like_action', TRUE);

	  // Checking the inputs
	  if (!empty($like_type) && in_array($like_type, $like_types_arr) && is_numeric($like_type_id) && ($like_action == '0' || $like_action == '1' || $like_action == '2')) {

		// Getting the post
		if ($like_type == 'tweet') {
		  $like_type_data = $this->tweet_model->get_tweet_by_id($like_type_id);
		} else if ($like_type == 'reply') {
		  $like_type_data = $this->tweet_model->get_reply_by_id($like_type_id);
		} else if ($like_type == 'event') {
		  $like_type_data = $this->event_model->get_event_by_id($like_type_id);
		} else if ($like_type == 'album') {
		  $like_type_data = $this->album_model->get_album_by_id($like_type_id);
		} else if ($like_type == 'photo' || $like_type == 'video') {
		  $like_type_data = $this->album_model->get_photo_by_id($like_type_id);
		} else if ($like_type == 'wall_post') {
		  $like_type_data = $this->wall_model->get_wall_post_by_id($like_type_id);
		} else if ($like_type == 'poll') {
		  $like_type_data = $this->poll_model->get_poll_by_id($like_type_id, NULL);
		} else if ($like_type == 'comment') {
		  $like_type_data = $this->comment_model->get_comment_by_id($like_type_id);
		} else {
		  $like_type_data = array('status' => '', 'result' => '');
		}

		// Checking weather post exists
		if ($like_type_data['status'] == 1 && !empty($like_type_data['result'])) {

		  // Like a post
		  if ($like_action == '1') {
			$add_like = $this->like_model->add_like($like_type, $like_type_id, $this->current_user['user_id'], 1);
			if ($add_like['status'] == 1 && !empty($add_like['result'])) {

			  // Add notification
			  if (!empty($like_type_data['result']['user_id']) && ($like_type_data['result']['user_id'] != $this->current_user['user_id'])) {
				$toast_text = '@' . $this->current_user['user_name'] . ' likes your ' . $like_type . ' <b>#' . make_post_id($like_type, $like_type_id) . '</b>';
				@add_toast_notificaion($like_type_data['result']['user_id'], $toast_text, make_post_id($like_type, $like_type_id));
			  }

			  $response['status'] = '1';
			}
		  }

		  // Unlike a post
		  if ($like_action == '0') {
			$del_like = $this->like_model->del_like($like_type, $like_type_id, $this->current_user['user_id']);
			if ($del_like['status'] == 1 && !empty($del_like['result'])) {
			  $response['status'] = '1';
			}
		  }

		  // Getting total likes count
		  $get_like = $this->like_model->get_like($like_type, $like_type_id);
		  if ($get_like['status'] == 1 && !empty($get_like['result'])) {
			$response['count'] = $get_like['count'];
		  }

		  // Getting the current users like status
		  $get_user_like = $this->like_model->get_like($like_type, $like_type_id, $this->current_user['user_id']);
		  if ($get_user_like['status'] == 1 && !empty($get_user_like['result'])) {
			$response['my_option'] = 1;
		  } else {
			$response['my_option'] = 0;
		  }
		}
	  }

	  // Responding json
	  die(json_encode($response));
	}

	// Redirect if something wrong
	redirect();
  }

  /**
   * Show liked users of a post (Ajax)
   * 
   */
  public function liked_users() {

	// Checking request type
	if ($this->is_post() && $this->is_ajax()) {

	  // Init vars
	  $like_types_arr = array('tweet', 'reply', 'event', 'album', 'photo', 'video', 'wall_post', 'poll', 'comment');

	  // Getting inputs
	  $like_type = $this->input->post('like_type', TRUE);
	  $like_type_id = $this->input->post('like_type_id', TRUE);

	  // Checking inputs
	  if (!empty($like_type) && in_array($like_type, $like_types_arr) && !empty($like_type_id) && is_numeric($like_type_id)) {

		// Getting the post
		if ($like_type == 'tweet') {
		  $like_type_data = $this->tweet_model->get_tweet_by_id($like_type_id);
		} else if ($like_type == 'reply') {
		  $like_type_data = $this->tweet_model->get_reply_by_id($like_type_id);
		} else if ($like_type == 'event') {
		  $like_type_data = $this->event_model->get_event_by_id($like_type_id);
		} else if ($like_type == 'album') {
		  $like_type_data = $this->album_model->get_album_by_id($like_type_id);
		} else if ($like_type == 'photo' || $like_type == 'video') {
		  $like_type_data = $this->album_model->get_photo_by_id($like_type_id);
		} else if ($like_type == 'wall_post') {
		  $like_type_data = $this->wall_model->get_wall_post_by_id($like_type_id);
		} else if ($like_type == 'poll') {
		  $like_type_data = $this->poll_model->get_poll_by_id($like_type_id, NULL);
		} else if ($like_type == 'comment') {
		  $like_type_data = $this->comment_model->get_comment_by_id($like_type_id);
		} else {
		  $like_type_data = array('status' => '', 'result' => '');
		}

		// Checking the post details
		if ($like_type_data['status'] == 1 && !empty($like_type_data['result'])) {

		  // Getting liked users
		  $get_like_users = $this->like_model->get_like_users($like_type, $like_type_id);

		  // Checking liked users data
		  if ($get_like_users['status'] == 1 && !empty($get_like_users['result'])) {

			// Preparing & Loading view
			$view_data = array();
			$view_data['like_type_data'] = $like_type_data;
			$view_data['like_user_data'] = $get_like_users['result'];
			$this->load->view('like_liked_users', $view_data);
			return;
		  }
		}
	  }

	  // Return if no users
	  return;
	}

	// Redirect if invalid request
	redirect();
  }

  /**
   * Update likes of posts (Ajax)
   * 
   */
  public function update_likes_count() {

	// Checking request type
	if ($this->is_post() && $this->is_ajax()) {

	  // Init vars
	  $like_button_details = array();

	  // Getting inputs
	  $like_buttons = $this->input->post('like_buttons');

	  // Checking inputs
	  if (!empty($like_buttons) && is_array($like_buttons)) {

		// Processing each like button
		foreach ($like_buttons as $like_button) {

		  // Getting a like button detail
		  if (isset($like_button[0])) {
			$like_type = $like_button[0];
		  } else {
			$like_type = NULL;
		  }
		  if (isset($like_button[1])) {
			$like_type_id = $like_button[1];
		  } else {
			$like_type_id = NULL;
		  }

		  // Checking like button detail
		  if (!empty($like_type) && !empty($like_type_id)) {

			// Total likes of this like button
			$total_likes_count = 0;
			$get_like = $this->like_model->get_like($like_type, $like_type_id);
			if ($get_like['status'] == 1 && !empty($get_like['result'])) {
			  $total_likes_count = $get_like['count'];
			}

			// Current users option of like button
			$my_like_status = 0;
			$get_user_like = $this->like_model->get_like($like_type, $like_type_id, $this->current_user['user_id']);
			if ($get_user_like['status'] == 1 && !empty($get_user_like['result'])) {
			  $my_like_status = 1;
			}

			// Injecting to response
			$like_button_details[] = array('like_type' => $like_type, 'like_type_id' => $like_type_id, 'total_likes_count' => $total_likes_count, 'my_like_status' => $my_like_status);
		  }
		}
	  }

	  // Responding json
	  die(json_encode($like_button_details));
	}

	// Redirect if something wrong
	redirect();
  }

}

?>